package com.linkelee.server.controller.oss;

import com.google.gson.Gson;
import com.linkelee.api.constant.CloudConfigConstant;
import com.linkelee.api.enums.CloudServiceEnum;
import com.linkelee.api.exception.OverAllException;
import com.linkelee.api.model.base.JsonResult;
import com.linkelee.api.model.oss.SysOss;
import com.linkelee.api.service.oss.ISysOssService;
import com.linkelee.api.service.sys.ISysConfigService;
import com.linkelee.api.util.PageUtils;
import com.linkelee.api.util.ValidatorUtils;
import com.linkelee.api.validator.AliyunValidate;
import com.linkelee.api.validator.QiniuyunValidate;
import com.linkelee.api.validator.TxyunValidate;
import com.linkelee.biz.service.oss.cloud.CloudStorageConfig;
import com.linkelee.biz.service.oss.cloud.OSSFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 文件上传
 *
 * @author lsl 154751575@qq.com
 */
@RestController
@RequestMapping("sys/oss")
public class SysOssController {
	@Autowired
	private ISysOssService sysOssService;
    @Autowired
    private ISysConfigService sysConfigService;

    private final static String KEY =
			CloudConfigConstant.CLOUD_STORAGE_CONFIG_KEY;
	
	/**
	 * 列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:oss:all")
	public JsonResult list(@RequestParam Map<String, Object> params){
		PageUtils page = sysOssService.queryPage(params);
		return JsonResult.ok().put("page", page);
	}


    /**
     * 云存储配置信息
     */
    @GetMapping("/config")
    @RequiresPermissions("sys:oss:all")
    public JsonResult config(){
        CloudStorageConfig config =
				sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);
        return JsonResult.ok().put("config", config);
    }


	/**
	 * 保存云存储配置信息
	 */
	@PostMapping("/saveConfig")
	@RequiresPermissions("sys:oss:all")
	public JsonResult saveConfig(@RequestBody CloudStorageConfig config){
		//校验类型
		ValidatorUtils.validateEntity(config);

		if(config.getType() == CloudServiceEnum.QINIU.getValue()){
			//校验七牛数据
			ValidatorUtils.validateEntity(config, QiniuyunValidate.class);
		}else if(config.getType() == CloudServiceEnum.ALIYUN.getValue()){
			//校验阿里云数据
			ValidatorUtils.validateEntity(config, AliyunValidate.class);
		}else if(config.getType() == CloudServiceEnum.QCLOUD.getValue()){
			//校验腾讯云数据
			ValidatorUtils.validateEntity(config, TxyunValidate.class);
		}

        sysConfigService.updateValueByKey(KEY, new Gson().toJson(config));

		return JsonResult.ok();
	}
	

	/**
	 * 上传文件
	 */
	@PostMapping("/upload")
	@RequiresPermissions("sys:oss:all")
	public JsonResult upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			throw new OverAllException("上传文件不能为空");
		}

		//上传文件
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

		//保存文件信息
		SysOss ossEntity = new SysOss();
		ossEntity.setUrl(url);
		ossEntity.setCreateDate(new Date());
		sysOssService.save(ossEntity);

		return JsonResult.ok().put("url", url);
	}


	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@RequiresPermissions("sys:oss:all")
	public JsonResult delete(@RequestBody Long[] ids){
		sysOssService.removeByIds(Arrays.asList(ids));

		return JsonResult.ok();
	}

}
